package com.tandn.activity.show;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.tandn.AppMain;
import com.tandn.R;
import com.tandn.helpers.MapOverlay;
import com.tandn.models.ATM;

public class ShowMapActivity extends MapActivity
{
	private MapView _mapView = null;
	private Geocoder _geocoder = null;
	private MapController _mapController = null;
	private List<Overlay> _overlaysList = null;

	@Override
	protected void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		setContentView(R.layout.show_map);

		final TextView textBankName = (TextView) findViewById(R.id.textBankMap);
		final TextView textBankAddress = (TextView) findViewById(R.id.textBankAddressMap);

		_mapView = (MapView) findViewById(R.id.mapviewShow);
		_overlaysList = _mapView.getOverlays();
		_geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
		_mapController = _mapView.getController();

		/* Add text title */
		textBankName.setText(getString(R.string.textViewBank) + " "
				+ AppMain._bankName);
		if (AppMain._listATMSearchWithDistrict != null)
		{
			/* Add text address */
			textBankAddress.setText(getString(R.string.textViewDistrict) + " "
					+ AppMain._listATMSearchWithDistrict.get(0).getDistrict()
					+ ", " + getString(R.string.textViewCity) + " "
					+ AppMain._listATMSearchWithDistrict.get(0).getCity());
			/* Get geopoint for atms */
			getATMGeo(AppMain._listATMSearchWithDistrict);
			/* Go to the city */
			animateTo(AppMain._listATMSearchWithDistrict.get(0).getDistrict()
					+ ", " + AppMain._listATMSearchWithCity.get(0).getCity());
		}
		else
		{
			/* Add text address */
			textBankAddress.setText(getString(R.string.textViewCity) + " "
					+ AppMain._listATMSearchWithCity.get(0).getCity());
			/* Get geopoint for atms */
			getATMGeo(AppMain._listATMSearchWithCity);
			/* Go to the city */
			animateTo(AppMain._listATMSearchWithCity.get(0).getCity());
		}

		_mapView.setBuiltInZoomControls(true);
		_mapController.setZoom(14);
		_mapView.invalidate();
	}

	private void getATMGeo(List<ATM> atmsList)
	{
		for (ATM atm : atmsList)
		{
			try
			{
				if (atm.getGeo() == null)
				{
					List<Address> addressesList = _geocoder.getFromLocationName(atm.getPlace(), 1);
					if (addressesList.size() > 0)
					{
						atm.setGeo(new GeoPoint(
								(int) (addressesList.get(0).getLatitude() * 1e6),
								(int) (addressesList.get(0).getLongitude() * 1e6)));
					}
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}

			MapOverlay mapOverlay = new MapOverlay(ShowMapActivity.this,
					atm.getGeo(), R.drawable.marker);
			Log.d("Debug", "1111111 " + atm.getGeo().getLatitudeE6() + ", " + atm.getGeo().getLongitudeE6());
			_overlaysList.add(mapOverlay);
		}

	}

	private void animateTo(String location)
	{
		List<Address> addressesList;
		try
		{
			addressesList = _geocoder.getFromLocationName(location, 1);
			if (addressesList.size() > 0)
			{
				_mapController.animateTo(new GeoPoint(
						(int) (addressesList.get(0).getLatitude() * 1e6),
						(int) (addressesList.get(0).getLongitude() * 1e6)));
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}

}
